package com.beiding.jsonanalyzer.querylink;

import com.beiding.jsonanalyzer.flow.ValueNode;
import com.beiding.jsonanalyzer.flow.BranchNode;
import com.beiding.jsonanalyzer.visit.VisitAble;

public abstract class QueryLinkNodeAdapter<W extends ValueNode> implements QueryLinkNode<W> {

    private QueryLinkNode<W> pre;

    public QueryLinkNodeAdapter(QueryLinkNode<W> pre) {
        this.pre = pre;
    }


    @Override
    public QueryLinkNodeResult<W> exec() {

        //获取前置节点执行结果
        QueryLinkNodeResult<W> exec = pre.exec();

        //获取前置节点产生的流程分支
        VisitAble<BranchNode<W>> branches = exec.getBranches();

        //转换当前流程分支
        VisitAble<BranchNode<W>> valueNodeVisitAble = doExec(branches);

        //返回执行结果
        return new QueryLinkNodeResult<W>(valueNodeVisitAble,exec.getFlow());

    }

    /**
     * 将前节点生成的流程分支转换成新的流程分支
     * @param preResult 前一个流程分支
     * @return 转换后的结果
     */
    protected abstract VisitAble<BranchNode<W>> doExec(VisitAble<BranchNode<W>> preResult);

}
